package com.example.downloader;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.downloader.adapter.DownloadAdapter;
import com.example.downloader.http.SimpleOkHttpDownloader;
import com.example.downloader.model.BaseDownloader;
import com.example.downloader.model.DownloadInfo;
import com.example.downloader.model.ProgressHolder;

import java.util.ArrayList;
import java.util.List;

import static com.example.downloader.model.DownloadCallback.FLAG_CANCLED;
import static com.example.downloader.model.DownloadCallback.FLAG_DOWNLOADING;
import static com.example.downloader.model.DownloadCallback.FLAG_ERROR;
import static com.example.downloader.model.DownloadCallback.FLAG_FINISHED;
import static com.example.downloader.model.DownloadCallback.FLAG_FREE;
import static com.example.downloader.model.DownloadCallback.FLAG_READY;

public class OKHttpDownloadActivity extends BaseActivity {
    private ListView mListView;
    private static final String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getPath() + "/OkHttpDownload";
    private BaseDownloader mDownloader;
    private static String[] sList = {
            "asdfasdf",
//            "http://map.ps123.net/china/UploadFile/201407/2014070323542296.jpg",
            "http://img.daimg.com/uploads/allimg/171122/3-1G1221AK3.jpg",
            "http://seopic.699pic.com/photo/40007/2283.jpg_wh1200.jpg",
            "http://pic.sc.chinaz.com/files/pic/pic9/201711/bpic4327.jpg",
            "http://pic1.sc.chinaz.com/files/pic/pic9/201711/bpic4328.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4329.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4330.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4331.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4332.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4333.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4334.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4335.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4336.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4337.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4338.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4339.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4340.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4341.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4342.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4343.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4344.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4345.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4346.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4347.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4348.jpg",
            "http://pics.sc.chinaz.com/files/pic/pic9/201711/bpic4349.jpg",};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp_download);
        mDownloader = new SimpleOkHttpDownloader(DOWNLOAD_PATH);
        mListView = (ListView) findViewById(R.id.listview);
        findViewById(R.id.cancel_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDownloader.cancleAllWork(false);
            }
        });

        List<DownloadInfo> infos = new ArrayList<>();
        for (int i = 0; i < sList.length; ++i) {
            infos.add(new DownloadInfo(sList[i], "file" + i + ".jpg"));
        }

        DownloadAdapter adapter = new DownloadAdapter(this, infos, mDownloader);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final DownloadInfo info = (DownloadInfo) parent.getAdapter().getItem(position);
                switch (mDownloader.getWorkingState(info.uri)) {
                    case FLAG_FREE:
                        mDownloader.downloadFile(new DownloadAdapter.DwonloadAware((ProgressHolder) view.getTag()), info);
                        break;
                    case FLAG_READY:
                        showToast("不要急~正在排队~~~~");
                        break;
                    case FLAG_ERROR:
                        showDialog("下载错误，是否重新下载?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mDownloader.downloadFile(new DownloadAdapter.DwonloadAware((ProgressHolder) view.getTag()), info);
                            }
                        });
                        break;
                    case FLAG_CANCLED:
                        showDialog("已取消,是否重新下载?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mDownloader.downloadFile(new DownloadAdapter.DwonloadAware((ProgressHolder) view.getTag()), info);
                            }
                        });

                        break;
                    case FLAG_DOWNLOADING:
                        if (mDownloader.cancle(info.uri)) {
                            showToast(info.name + "已取消");
                        }
                        break;
                    case FLAG_FINISHED:
                        showDialog("下载完成,是否重新下载?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mDownloader.downloadFile(new DownloadAdapter.DwonloadAware((ProgressHolder) view.getTag()), info);
                            }
                        });
                        break;
                }


            }
        });

    }

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, OKHttpDownloadActivity.class);
        context.startActivity(intent);
    }
}
